package com.example.fish.controller;

import com.example.fish.pojo.User;
import com.example.fish.service.SecretService;
import com.example.fish.utils.JsonResult;
import com.example.fish.vo.SecretList;
import com.example.fish.vo.TitleSecret;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/secret")
public class SecretController extends BaseController{
    @Autowired
    private SecretService secretService;
//    保存secret的接口
    @PostMapping("/publish")
    private JsonResult<Void> save(HttpSession session,String title,String type,String content){
        User user =(User) session.getAttribute("user");
        secretService.publishSecret(user.getUserid(),title,type,content);
        return new JsonResult<>(200,"success",null);
    }

//    秘籍页面的分页查询
    @PostMapping("/page")
    private JsonResult<PageInfo<SecretList>> page(Integer pageNum){
        PageInfo<SecretList> pageInfo = secretService.getAllSecretByPage(pageNum);
        return new JsonResult<>(200,"success",pageInfo);
    }

//    search的模糊查询
    @PostMapping("/search")
    private JsonResult<PageInfo<SecretList>> search(String keyword,Integer pageNum){
        PageInfo<SecretList> pageInfo = secretService.getSecretByKeyword(pageNum, keyword);
        return new JsonResult<>(200,"success",pageInfo);
    }
//    喜欢不喜欢
    @PostMapping("/agree")
    private JsonResult<Void> agree(Integer secretid){
        secretService.setAgree(secretid);
        return new JsonResult<>(200,"success",null);
    }
    @PostMapping("/cancelagree")
    private JsonResult<Void> cagree(Integer secretid){
        secretService.cancelAgree(secretid);
        return new JsonResult<>(200,"success",null);
    }
    @PostMapping("/disagree")
    private JsonResult<Void> disagree(Integer secretid){
        secretService.setDisagree(secretid);
        return new JsonResult<>(200,"success",null);
    }
    @PostMapping("/canceldisagree")
    private JsonResult<Void> cdisagree(Integer secretid){
        secretService.cancelDisagree(secretid);
        return new JsonResult<>(200,"success",null);
    }
    @PostMapping("/detail")
    private JsonResult<SecretList> detail(Integer secretid){
        SecretList info = secretService.getSecretBySecretid(secretid);
        return new JsonResult<>(200,"success",info);
    }

//    前五的文章
    @GetMapping("/getTitle")
    private JsonResult<List<TitleSecret>> getTitle(){
        List<TitleSecret> list = secretService.titleList();
        return new JsonResult<>(200,"success",list);
    }
}
